package events

import (
	"context"
	"encoding/json"
	"github.com/IBM/sarama"
)

type Producer interface {
	ProduceSearchEvent(ctx context.Context, evt SyncDataEvent) error
}
type SearchKafkaProducer struct {
	producer sarama.AsyncProducer
}

func NewSearchKafkaProducer(pc sarama.AsyncProducer) Producer {
	return &SearchKafkaProducer{producer: pc}
}

func (k *SearchKafkaProducer) ProduceSearchEvent(ctx context.Context, evt SyncDataEvent) error {
	data, err := json.Marshal(evt)
	if err != nil {
		return err
	}
	k.producer.Input() <- &sarama.ProducerMessage{
		Topic: evt.Topic(),
		Value: sarama.StringEncoder(data),
	}
	return nil
}
func (k *SearchKafkaProducer) Close() error {
	return k.producer.Close()
}

type SyncDataEvent struct {
	IndexName string
	DocID     string
	Data      string
}

func (SyncDataEvent) Topic() string {
	return "search_tags"
}
